import axios from "axios"

//axios拦截器的封装  设置基准路由和错误处理
//判断开发还是生产环境
const isDEV = process.env.NODE_ENV === "development"
    // console.log(process.env.NODE_ENV);
axios.interceptors.request.use(
        config => {
            config.baseURL = "/api"
                //定义token
            const token = localStorage.getItem("token")
            if (token) {
                config.headers.authorization = `Bearer ${token}`
            }
            return config
        },

        err => {
            if (isDEV) {
                //如果是生产环境就打印这些
                console.log("err", err.response.data);
            }
            return Promise.reject(err.response.data)
        }
    )
    //响应拦截
axios.interceptors.response.use(
        res => {
            // console.log(res)
            return res.data
        },
        err => {
            console.log("我是拦截器中的err", err.response.data)
            return Promise.reject(err.response.data)
        }
    )
    //请求延时1.5秒
export const lazyRequest = (request, deply = 1500) => {
    const promise = new Promise(resolve => {
        setTimeout(resolve, deply)
    })
    return Promise.all([request, promise])
}


export default axios